import Vue from 'vue'
import ajax from '@U/ajax'
Vue.use(ajax)

export default {
  key:"COMMON-KEY",
  get(){
    var common = {};
    try{
      common = JSON.parse(localStorage.getItem(this.key)) || {};
    }catch(e){

    }
    return common;
  },
  setItem(key,value){
    var common = this.get();
    common[key] = value;
    localStorage.setItem(this.key,JSON.stringify(common));
  },
  getItem(key){
    var common = this.get();
    return common[key];
  },
  getSystemNameForCommon(cb){
    var name = this.getItem("systemName");
    if(name){
      cb && cb(name);
    }
    ajax.get("/admin/auth/common/authCommon/get.do",{
      success:(res) => {
        var object = (res.object || {});
        for(var key in object){
          this.setItem(key,object[key]);
          cb && cb(this.get());
        }
      }
    });
  },
  getSystemNameForUser(cb){
    var name = this.getItem("systemName");
    if(name){
      cb && cb(name);
    }
    ajax.get("/admin/auth/user/authCommon/get.do",{
      success:(res) => {
        var object = (res.object || {});
        for(var key in object){
          this.setItem(key,object[key]);
          cb && cb(this.get());
        }
      }
    });
  },
  getSystemNameForInstance(cb){
    var name = this.getItem("instance_name");
    if(name){
      cb && cb(this.get());
    }
    ajax.get("/admin/auth/instance/authCommon/get.do",{
      success:(res) => {
        var object = (res.object || {});
        for(var key in object){
          this.setItem(key,object[key]);
          cb && cb(this.get());
        }
      }
    });
  }
};